package com.leaves.javamall.product.service.impl;

import com.leaves.javamall.product.service.CategoryBrandRelationService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leaves.common.utils.PageUtils;
import com.leaves.common.utils.Query;

import com.leaves.javamall.product.dao.CategoryDao;
import com.leaves.javamall.product.entity.CategoryEntity;
import com.leaves.javamall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

//    @Autowired
//    CategoryDao categoryDao;
    @Autowired
    CategoryService categoryService;
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //查出所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //找到所有一级分类
        List<CategoryEntity> level1Menus = categoryEntities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0 ).map((menu) ->{
            menu.setChildren(getChildren(menu,categoryEntities));
            return menu;
        }).sorted((menu1,menu2)->{
            return menu1.getSort() -menu2.getSort();
        }).collect(Collectors.toList());

        //返回查询结果
        return level1Menus;
    }

    @Override
    public void removeMuneIds(List<Long> asList) {
        //ToDo 检查当前删除菜单是否被其他地方引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        System.out.println(catelogId); //535L
        List<Long> Paths = new ArrayList<>();
        List<Long> path = this.findPath(catelogId, Paths);
        Collections.reverse(path);
        return path.toArray(new Long[path.size()]);
    }

    @Override
    public void updateDetail(CategoryEntity entity) {
        this.updateById(entity);
        if(!StringUtils.isNullOrEmpty(entity.getName())){
            categoryBrandRelationService.updateCategoryDetails(entity.getName(),entity.getCatId());
        }
    }


    private List<Long> findPath(Long catelogId,List<Long> Paths){
        CategoryEntity categoryEntity = this.getById(catelogId);
        Paths.add(catelogId);
        if(categoryEntity.getParentCid()!=0){
            Long parentCid = categoryEntity.getParentCid();
            this.findPath(parentCid,Paths);
        }
        return Paths;

    }


    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildren(CategoryEntity entity,List<CategoryEntity> all){
        List<CategoryEntity> collect = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == entity.getCatId();
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null?0:menu1.getSort()) - (menu2.getSort() == null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return collect;
    }
}